package com.casework

class NaceController {

    static scaffold = true
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]


    def create = {
        def caseId = params.remove('caseId')
        def naceInstance = new Nace()
        naceInstance.properties = params
        return [naceInstance: naceInstance, caseId: caseId]
    }

    def save = {
        def naceInstance = new Nace(params)
        def legalCase = LegalCase.get(params.caseId)
        if (naceInstance.save(flush: true)) {
            if (legalCase) {
                LegalCaseNaceFullDescription.create(legalCase, naceInstance, true)
                redirect action:'show', id: legalCase.id, controller: 'legalCase'
                return
            }
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'nace.label', default: 'Nace'), naceInstance.id])}"
            redirect(action: "show", id: naceInstance.id)
        }
        else {
            render(view: "create", model: [naceInstance: naceInstance])
        }
    }

    def show = {
        def naceInstance = Nace.get(params.id)

        if (!naceInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'nace.label', default: 'Nace'), params.id])
            redirect(action: "list")
        }
        else {
            def legalCases = LegalCaseNaceFullDescription.findAllByDescription(naceInstance).collect { it.legalCase } as Set
           [naceInstance: naceInstance, legalCases: legalCases]
        }
    }

}
